<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\controller\ding;

use app\store\controller\Controller;
use app\store\model\ding\Seat as SeatModel;

/**
 * 文章管理控制器
 * Class Seat
 * @package app\store\controller\content
 */
class Seat extends Controller
{
    /**
     * 文章列表
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function list()
    {
        $model = new SeatModel;
        $list  = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    public function listAll()
    {
        $model = new SeatModel;
        $list  = $model->getListAll($this->request->param());
        return $this->renderSuccess(compact('list'));
    }
    //预订管理
    public function orderList()
    {
        $model = new SeatModel;
        $list  = $model->getListOrder($this->request->param());
        $date  = '';
        if ($this->request->param()['date'] == '') {
            $date = Date('Y-m-d');
        } else {
            $params = $this->request->param();
            $dd     = explode('T', $params['date']);

            $dd2 = explode(';', $dd[0]);
            if (count($dd2) > 1) {
                $params['date'] = $dd2[1];
                $date           = Date('Y-m-d', strtotime($params['date']) + 86400);
            } else {
                $date = $params['date'];
            }

        }
        return $this->renderSuccess(compact('list', 'date'));
    }
    public function orderList2()
    {
        $model = new SeatModel;
        $list  = $model->getListOrder($this->request->param());
        $date = Date('Y-m-d');
        return $this->renderSuccess(compact('list', 'date'));
    }
    /**
     * 文章详情
     * @param int $SeatId
     * @return array|bool|string
     */
    public function detail(int $seatId)
    {
        $detail = SeatModel::detail($seatId);
        // 获取image (这里不能用with因为编辑页需要image对象)
        ! empty($detail) && $detail['image'];
        ! empty($detail) && $detail['imagePoster'];
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 添加文章
     * @return array|mixed
     */
    public function add()
    {
        // 新增记录
        $model = new SeatModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    //占座
    public function zhanzuo()
    {
        $data  = $this->postForm();
        $model = new SeatModel;

        // 更新记录
        if ($model->lock($data)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    /**
     * 更新文章
     * @param int $SeatId
     * @return array|mixed
     */
    public function edit(int $seatId)
    {
        // 文章详情
        $model = SeatModel::detail($seatId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除文章
     * @param $SeatId
     * @return array
     */
    public function delete(int $seatId)
    {
        // 文章详情
        $model = SeatModel::detail($seatId);
        if (! $model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    public function getTime($type = 1)
    {
        $start_date = Date('2024-12-12 00:00');
        $start_date = strtotime($start_date);
        $time1      = [];
        $time       = [];
        for ($i = 1; $i < 31; $i++) {
            $time['label'] = Date('H:i', $start_date);
            $time['value'] = $i;
            $start_date += 30 * 60;
            $time1[] = $time;

        }

        $start_date = Date('2024-12-12 15:00');
        $start_date = strtotime($start_date);
        $time2      = [];
        $dd         = [];
        for ($i = 1; $i < 19; $i++) {
            $dd['label'] = Date('H:i', $start_date);
            $dd['value'] = $i;
            $start_date += 30 * 60;
            $time2[] = $dd;

        }

        $start_date = Date('2024-12-12 00:00');
        $start_date = strtotime($start_date);

        $time = [];
        for ($i = 1; $i < 49; $i++) {
            $time22['label'] = Date('H:i', $start_date);
            $time22['value'] = $i;
            $start_date += 30 * 60;

            $time[] = $time22;

        }

        if ($type == 1) {
            $time = $time1;
        } elseif ($type == 2) {
            $time = $time2;
        } else {
            $time = $time;
        }

        return $this->renderSuccess(compact('time', 'time1', 'time2'));
    }

}
